import React from 'react';
import CommonList from '../../../components/CommonLists/list';
import { IS_SYSTEM } from '../../../utils/constant';
import { Button, message } from 'antd';
import services from '../../../services/api';
import Modal from '../../../components/Modal/index';
import Enumeration from './enumeration'

class FieldList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'field';
    this.state = {
      applications: [],
      events: []
    };
    this.operatorWidth = '120px';
    this.editButton = record => {
      const reg = /^sys_\w+/;
      const disabled = record.is_system === IS_SYSTEM._TRUE && !reg.test(record.field_code);
      return (
        <Button
          size='small'
          type='link'
          onClick={this.edit.bind(this, record, { title: '编辑字段' })}
          disabled={disabled || record.audit_status === this.AUDIT._WAIT}
        >
          编辑
        </Button>
      );
    };
    this.delButton = record => {
      const reg = /^sys_\w+/;
      const disabled = record.is_system === IS_SYSTEM._TRUE && !reg.test(record.field_code);
      return (
        <Button
          size='small'
          type='link'
          disabled={disabled || record.audit_status === this.AUDIT._WAIT}
          onClick={this.delete.bind(this, record)}
        >
          删除
        </Button>
      );
    };
    this.enumerationButton = record => {
      const reg = /^sys_\w+/;
      const disabled = record.is_system === IS_SYSTEM._TRUE && !reg.test(record.field_code);
      return (
        <Button
          size='small'
          type='link'
          onClick={this.enumerationModel.bind(this, record)}
          disabled={disabled || record.audit_status === this.AUDIT._WAIT}
        >
          枚举
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.delButton, this.enumerationButton];
    this.enumerationRef = React.createRef()
  }

  // 删除时的参数
  getDeleteParam = record => {
    return { field_id: record.field_id };
  };

  delete(record) {
    if (record.strategy_count > 0) {
      message.error(`有${record.strategy_count}条策略使用了此字段，请先下线关联策略再删除此字段！`);
      return;
    }
    if (record.feature_count > 0) {
      message.error(`有${record.feature_count}条指标使用了此字段，请先在关联指标中移除此字段再删除此字段！`);
      return;
    }
    Modal.open({
      title: '',
      isConfirm: true,
      width: '300px',
      content: '确定要删除?',
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        services[this.api].delete(this.getDeleteParam(record)).then(data => {
          if (data.code === 0) {
            message.success(data.msg);
            this.props.queryList();
            this.props.clearSelected && this.props.clearSelected();
            Modal.close();
          } else {
            message.error(data.msg);
          }
        });
      },
      cancel: () => {
        Modal.close();
      }
    });
  }

  enumerationModel(record) {
    Modal.open({
      title: '枚举设置',
      width: '600px',
      content: <Enumeration field_id={record.field_id} createRef={this.enumerationRef}></Enumeration>,
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        this.enumerationRef.current.getValue((data) => {
          const params = {
            field_id: record.field_id,
            data
          }
          services[this.api].fieldEnumerationCreate(params).then(data => {
            if (data.code === 0) {
              message.success(data.msg);
              Modal.close();
            } else {
              message.error(data.msg);
            }
          });
        })
      },
      cancel: () => {
        Modal.close();
      }
    });
  }
}

export default FieldList;
